﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WindowsFormsApplication3;

namespace bsy
{
    public partial class Project : Form
    {
        private string connStr = Properties.Settings.Default.bsyConnectionString;

        public Project()
        {
            InitializeComponent();
        }

        public void Project_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            ShowProj();
            ShowFeesStan();
        }


        //左侧DataGridView中显示项目、功效、项目手工提成
        public void ShowProj()
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            string sql = "select item_id,name 项目,efficacy 功效,elevate 项目手工提成 from item order by item_id ";
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            this.Project_dgv1.DataSource = ds.Tables[0].DefaultView;
            this.Project_dgv1.Columns[0].Visible = false;
            ds.Dispose();
            conn.Close();
        }
        
        //右侧显示建议收费标准
        public void ShowFeesStan()
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            String sql1 = "select count(*) from item";
            DataSet dst = new DataSet();
            dst = CommonDB.Query(sql1);
            int count = (int)dst.Tables[0].Rows[0][0];
            if (count == 0)
            {
                return;
            }
            else
            {
                int item_id = 0;
                if (string.Empty != Project_dgv1.Rows[0].Cells[0].Value)
                {
                    item_id = int.Parse(Project_dgv1.Rows[0].Cells[0].Value.ToString());
                }

                string sql2 = string.Format("select ric.card_id 卡ID,name 项目卡类型,ric.price 价格 from cart,Re_item_cart ric where type='{0}' and cart.cart_id=ric.card_id and ric.item_id={1} order by cart.cart_id", "xiangmuka", item_id);
                SqlDataAdapter sda = new SqlDataAdapter(sql2, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                this.Project_dgv2.DataSource = ds.Tables[0].DefaultView;
                this.Project_dgv2.Columns[0].Visible = false;
            }
            dst.Dispose();
            conn.Close();
        }


        private void Project_button1_Click(object sender, EventArgs e)
        {
            String ProjName = Project_textBox1.Text.Trim();
            String Projefficacy = Project_textBox3.Text.Trim();
            if (ProjName == "")
            {
                MessageBox.Show("请输入完整数据再保存！","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            try
            {
                float ProjComm = float.Parse(Project_textBox2.Text.Trim());

                //检查数据库中是否存在该输入的记录
                String sql1 = "select count(*) from item where name='"+ProjName+"'";
                DataSet ds1 = new DataSet();
                ds1 = CommonDB.Query(sql1);
                int temp1 = (int)ds1.Tables[0].Rows[0][0];
                if (temp1 == 0)
                {
                    String sql2 = "insert into item values('" + ProjName + "','" + Projefficacy + "','" + ProjComm + "')";
                    CommonDB.ExecuteSql(sql2);
                    MessageBox.Show("保存成功！","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    //保存数据成功后将文本框设置为空
                    Project_textBox1.Text = "";
                    Project_textBox2.Text = "";
                    Project_textBox3.Text = "";

                    //取出新录入项目的项目id
                    String sql3 = "select item_id from item where name='" + ProjName + "'";
                    DataSet ds2 = new DataSet();
                    ds2 = CommonDB.Query(sql3);
                    int item_id = (int)ds2.Tables[0].Rows[0][0];

                    SqlConnection conn = new SqlConnection(connStr);
                    conn.Open();
                    String sql4 = "select cart_id from cart where type='xiangmuka'";
                    SqlDataAdapter sda = new SqlDataAdapter(sql4, conn);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        String card_id = ds.Tables[0].Rows[i][0].ToString();
                        String sql5 = "insert into Re_item_cart values('" + item_id + "','" + card_id + "',0)";
                        CommonDB.ExecuteSql(sql5);
                    }
                    ds.Dispose();
                    ds2.Dispose();
                    conn.Close();
                }
                else
                {
                    MessageBox.Show("该项目已存在！","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    return;
                }
                ds1.Dispose();
            }
            catch(FormatException FE)
            {
                MessageBox.Show("请检查项目手工提成的输入值！","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            Project_Load(sender, e);
        }

        private void Proj_button2_Click(object sender, EventArgs e)
        {
            //循环将项目卡种类相应的价格和相应的项目id插入数据库Re_item_cart表中
            for (int i = 0; i < Project_dgv2.RowCount; i++)
            {
                String item_id = Project_dgv1.CurrentRow.Cells["item_id"].FormattedValue.ToString();//在Project_dgv1中取出item_id
                String card_id = Project_dgv2.Rows[i].Cells[0].FormattedValue.ToString();//在Project_dgv2中取出卡ID
                try
                {
                    float price = float.Parse(Project_dgv2.Rows[i].Cells[2].EditedFormattedValue.ToString());
                    String sql1 = "update Re_item_cart set price='" + price + "' where item_id='" + item_id + "' and card_id='" + card_id + "'";
                    CommonDB.ExecuteSql(sql1);
                }
                catch(FormatException f)
                {

                    MessageBox.Show("如无需定义价格，请置为0！否则系统将以原来的值进行保存！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            MessageBox.Show("保存成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //Project_dgv1单元格中的单击事件，获取Project_dgv1中选择当前行隐藏列的item_id，然后根据item_id在Project_dgv2中显示项目对应的各种卡的信息
        private void Project_dgv1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                SqlConnection conn = new SqlConnection(connStr);
                conn.Open();
                //判断Re_item_cart表中是否存在相对应item_id的项目
                String item_id = Project_dgv1.CurrentRow.Cells["item_id"].FormattedValue.ToString();//在Project_dgv1中取出item_id
                String sql2 = "select cart.cart_id 卡ID,name 项目卡类型,ric.price 价格 from cart,Re_item_cart ric where type='xiangmuka' and cart.cart_id=ric.card_id and ric.item_id='" + item_id + "' order by cart.cart_id";
                SqlDataAdapter sda = new SqlDataAdapter(sql2, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                this.Project_dgv2.DataSource = ds.Tables[0].DefaultView;
                this.Project_dgv2.Columns[0].Visible = false;
                ds.Dispose();
                conn.Close();
                this.belongTo.Text = this.Project_dgv1.Rows[e.RowIndex].Cells[1].Value.ToString();//设置项目类型
            }
        }

        //限制Project_dgv2中项目卡类型一列不能编辑,价格一列可编辑
        private void Project_dgv2_DoubleClick(object sender, EventArgs e)
        {
            Project_dgv2.Columns[1].ReadOnly = true;
        }
        private void Project_dgv2_Click(object sender, EventArgs e)
        {
            Project_dgv2.Columns[1].ReadOnly = true;
        }

        private void Project_dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            String item_id = Project_dgv1.CurrentRow.Cells["item_id"].FormattedValue.ToString();
            String name = Project_dgv1.CurrentRow.Cells[1].EditedFormattedValue.ToString();
            String efficacy = Project_dgv1.CurrentRow.Cells[2].EditedFormattedValue.ToString();
            if (name == "")
            {
                MessageBox.Show("项目名不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (Project_dgv1.CurrentRow.Cells[3].EditedFormattedValue.ToString() == "")
            {
                MessageBox.Show("如无需对此项目定义手工提成，请置为0！否则系统将以原来的值进行保存！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                
                float elevate = float.Parse(Project_dgv1.CurrentRow.Cells[3].EditedFormattedValue.ToString());
                if (Project_dgv1.CurrentCellAddress.X == 1)
                {
                    String sql1 = "select count(*) from item where name='" + name + "'";
                    DataSet ds = new DataSet();
                    ds = CommonDB.Query(sql1);
                    int temp = (int)ds.Tables[0].Rows[0][0];

                    if (temp == 0)
                    {
                        String updatasql = "update item set name='" + name + "' where item_id='" + item_id + "'";
                        CommonDB.ExecuteSql(updatasql);
                        MessageBox.Show("修改成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        String sql2 = "select item_id from item where name='" + name + "'";
                        DataSet ds2 = new DataSet();
                        ds2 = CommonDB.Query(sql2);
                        int it_id = (int)ds2.Tables[0].Rows[0][0];

                        int itemid = int.Parse(Project_dgv1.CurrentRow.Cells[0].EditedFormattedValue.ToString());

                        if (temp == 1 && itemid != it_id)
                        {
                            MessageBox.Show("该项目已存在，不能修改为同名项目！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            //return;
                        }
                        ds2.Dispose();
                        ds.Dispose();
                    }
                }
                else
                {
                    String sql1 = "update item set name='" + name + "' where item_id='" + item_id + "'";
                    String sql2 = "update item set efficacy='" + efficacy + "' where item_id='" + item_id + "'";
                    String sql3 = "update item set elevate='" + elevate + "' where item_id='" + item_id + "'";
                    CommonDB.ExecuteSql(sql1);
                    CommonDB.ExecuteSql(sql2);
                    CommonDB.ExecuteSql(sql3);
                    MessageBox.Show("修改成功！","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }  
            }
            
        }

        private void Project_dgv1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("项目手工提成输入有误！如不需对此项目定义手工提成，请输入0！否则系统将以原来的值进行保存！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void Project_dgv2_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("价格输入有误！如不需对此价格定义，请输入0！否则系统将以原来的值进行保存！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}
